home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / mpss / newplaye.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-20  |  6.0 KB  |  332 lines

  1.  
  2. /*******************************************************************************
  3. ********************************************************************************
  4. ********************************************************************************
  5.  
  6. PERMISSION TO COPY THIS SOFTWARE IS HEREBY GIVEN BY THE AUTHOR PROVIDED THAT
  7. THIS LEADING MESSAGE IS INCLUDED IN ALL OF THE RELEVANT SOURCE FILES.
  8.  
  9.         P. SCHMITZ, UNIVERSITY OF KEELE, MAY 1988.
  10.  
  11.  
  12. ********************************************************************************
  13. ********************************************************************************
  14. *******************************************************************************/
  15.  
  16. #include "files.h"
  17. #include "ships.h"
  18. #include "header.h"
  19. FILE *f, *fopen();
  20.  
  21. putmasterpidintofile()
  22. {
  23. int masterpid;
  24. char string[15];
  25.  
  26. masterpid=getpid();
  27. sprintf(string,"%d\n",masterpid);
  28. i=creat(MASTERPIDFILE,0666);
  29. if ((f=fopen(MASTERPIDFILE,"w"))==NULL) 
  30.             {
  31.             die();
  32.             } else
  33.             {
  34.             fputs(string,f); /*write master pid into file*/
  35.             fclose(f);
  36.             }
  37. }
  38.  
  39. find_player_id()
  40. {
  41. int i;
  42.  
  43. if (noofplayers>=10) 
  44.         {
  45.         return(0);
  46.         }
  47. i=0;
  48. while ((i<10)&&(players[i]==1)) ++i;
  49. players[i]=1;
  50. return(i);
  51. }
  52.  
  53. makenewplayerandjoinhimin()
  54. {
  55. char string[80];
  56. char *malloc(); 
  57. struct player *aa,*xp;
  58.  
  59. if (noofplayers==10)
  60. {
  61. unlink(LOCK);
  62. return(0);
  63. } else
  64. {
  65. if ((x=open(PLAYERFILE,0))<0) 
  66. {
  67. unlink(LOCK);
  68. return(0);
  69. }
  70. else
  71. {
  72. /* find a free player position */
  73. aa=(struct player *)malloc(sizeof(struct player));
  74.  
  75. /*NOW READ IN THE DATA FROM PLAYERFILE*/
  76. i=0;
  77. read(x,&c,1);
  78. while (c!='#')
  79.     {
  80.     if (i>13) {fprintf(stderr,"1:i went out:%d",i); die();}
  81.     aa->terminalfile[i]=c;
  82.     i++;
  83.     read(x,&c,1);
  84.     }
  85. i=0;
  86. read(x,&c,1);
  87. while (c!='#')
  88.     {
  89.     if (i>19) {fprintf(stderr,"2:i went out:%d",i); die();}
  90.     aa->cm[i]=c;
  91.     i++;
  92.     read(x,&c,1);
  93.     }
  94. i=0;
  95. read(x,&c,1);
  96. while (c!='#')
  97.     {
  98.     if (i>19) {fprintf(stderr,"3:i went out:%d",i); die();}
  99.     aa->cl[i]=c;
  100.     i++;
  101.     read(x,&c,1);
  102.     }
  103. i=0;
  104. read(x,&c,1);
  105. while (c!='#')
  106.     {
  107.     if (i>79) {fprintf(stderr,"4:i went out:%d",i); die();}
  108.     string[i]=c;
  109.     i++;
  110.     read(x,&c,1);
  111.     }
  112. for(y=i;y<80;y++) string[i]='\0';
  113. aa->shipno=atoi(string);
  114.  
  115. i=0;
  116. read(x,&c,1);
  117. while (c!='#')
  118.     {
  119.     if (i>79) {fprintf(stderr,"5:i went out:%d",i); die();}
  120.     string[i]=c;
  121.     i++;
  122.     read(x,&c,1);
  123.     }
  124. for(y=i;y<80;y++) string[i]='\0';
  125. aa->pid=atoi(string);
  126.  
  127. i=0;
  128. read(x,&c,1);
  129. while (c!='#')
  130.     {
  131.     if (i>79) {fprintf(stderr,"5:i went out:%d",i); die();}
  132.     string[i]=c;
  133.     i++;
  134.     read(x,&c,1);
  135.     }
  136. for(y=i;y<80;y++) string[i]='\0';
  137. aa->tty_type=atoi(string);
  138.  
  139. close(x);
  140. /*NOW READ IN THE DATA FROM PLAYERFILE*/
  141.  
  142. aa->terminalfile[13]=' ';
  143. /*
  144. fprintf(stderr,"shipno:%d\npid:%d\ntty_type:%d\n",aa->shipno,aa->pid,aa->tty_type);
  145. fprintf(stderr,"cm is :");
  146. for (i=0;i<20;i++) fprintf(stderr,"*%c*",aa->cm[i]); 
  147. fprintf(stderr,"\ncl is :");
  148. for (i=0;i<20;i++) fprintf(stderr,"*%c*",aa->cl[i]); 
  149. fprintf(stderr,"\nThats it\n");
  150. read(0,&c,1);
  151. */
  152. if (((aa->fd)=open((aa->terminalfile),2))==-1)
  153.     {
  154.     kill(aa->pid,9);
  155.     free(aa);
  156.     killcontrol();
  157.     } else
  158.     {
  159.     ++noofplayers;
  160.     aa->doneit=0;
  161.     aa->impulsemove=0;
  162.     aa->reason=10;
  163.     aa->id=(char)('a'+find_player_id());
  164.     aa->xpos=230;
  165.     aa->ypos=(int)(rnd(0,5)+675);
  166.     aa->oldxpos=aa->xpos;
  167.     aa->oldypos=aa->ypos;
  168.     aa->beeping=ON;
  169.     aa->maxvel=MAXWARP;
  170.     aa->xvel=0;
  171.     aa->yvel=0;
  172.     aa->chflg=FLAG;
  173.     aa->clflg=OFF;
  174.     aa->stat=3;
  175.     aa->energy=STARTENERGY;
  176.     aa->maxenergy=STARTENERGY;
  177.     aa->phaserbanks=STARTPHASER;
  178.     aa->maxphaser=STARTPHASER;
  179.     aa->photontorpedos=STARTPHOTON;
  180.     aa->maxphoton=STARTPHOTON;
  181.     aa->photonpower=PHOTONPOWER;
  182.     aa->phaserpower=PHASERPOWER;
  183.     aa->photonreach=WEAPONREACH;
  184.     aa->phaserreach=WEAPONREACH;
  185.     aa->weaponselect=0;
  186.     aa->score=0;
  187.     aa->cloakingdevice=OFF;
  188.     aa->shields=OFF;
  189.     for(k=0;k<30;++k)
  190.         aa->planets[k]=0;
  191.     aa->planetno=0;
  192.     for (k=0; k<=3; k++)
  193.         {
  194.         aa->channel[k].enemy=NULL;
  195.         aa->channel[k].xloc=0;
  196.         aa->channel[k].yloc=0;
  197.         }
  198.     aa->flyto= -1;
  199.     broadcast("They advise us that new players are coming into the game.");
  200.     flushbuf(aa);
  201.     addtolist(aa);
  202.     drawcross(aa);
  203.     plotdata(aa);
  204.     drawconsole(aa);
  205.     plot1(aa,"Permission to come aboard granted. Captain on the Bridge.");
  206.     plot2(aa,"              Welcome to the                             ");
  207.     poscurs(aa,49,22);
  208.     write(aa->fd,ship[aa->shipno].name,20);
  209.     
  210.     } /*else*/
  211. } /*else*/
  212. } /*else*/
  213. unlink(PLAYERFILE);
  214. unlink(LOCK);
  215.     
  216. /*BLOW UP ALL ALIENS*/
  217.  
  218. if (noofplayers==2)
  219.     {
  220.     xp=startlist;
  221.     while (xp!=NULL)
  222.             {
  223.             if ((xp->id==ROMID)||(xp->id==KLINGID))
  224.                         finish(xp);
  225.             xp=xp->next;
  226.             }
  227.     }
  228. }
  229.  
  230. addtolist(x)
  231. struct player *x;
  232. {
  233. if ((startlist==NULL) && (taillist==NULL))
  234. {
  235. startlist=x; taillist=x; (startlist->next)=NULL;
  236. } else
  237. {
  238. x->next=NULL;
  239. taillist->next=x;
  240. taillist=x;
  241. }
  242. }
  243.  
  244. remove_player_from_list(q)
  245. register struct player *q;
  246. {
  247. FILE *f,*fopen();
  248. struct player *a,*b; 
  249. struct torpedo *t;
  250. char string[50];
  251. int listlength,l,alien;
  252.  
  253. alien=0;
  254. if ((q->id==ROMID)||(q->id==KLINGID))  alien=1;
  255.  
  256. if (alien) goto skipfile;
  257.  
  258. while ((f=fopen(PLAYERSCOREFILE,"r"))!=NULL) 
  259.     { 
  260.     fclose(f); 
  261.     }
  262.     i=creat(PLAYERSCOREFILE,0666);
  263.     if ((f=fopen(PLAYERSCOREFILE,"w"))==NULL)
  264.     fprintf(stderr,"newplayer.c : Cant open   PLAYERSCOREFILE.\n");
  265.         sprintf(string,"%d %d\n",q->reason,q->score);
  266.     fputs(string,f);
  267.     fclose(f);
  268.  
  269. kill(q->pid,9); /*this kills the users dummy process*/
  270. /*make his playerid available again.*/
  271. if ((q->id>='A')&&(q->id<='Z')) q->id=(char)((q->id)+32);
  272. l=(int)((q->id)-'a');
  273. players[l]=0;
  274. close(q->fd);
  275. /*make his playerid available again.*/
  276.  
  277. skipfile :
  278.  
  279. listlength=noofplayers+noofaliens;
  280.  
  281. if (listlength>1)
  282. {
  283. t=tstartlist;
  284. while (t!=NULL)
  285.     {
  286.     if (t->whofrom==q) t->whofrom=NULL;
  287.     t=t->next;
  288.     }
  289.  
  290. b=startlist;
  291. while (b!=NULL)
  292.     {
  293.     for (i=0; i<5; i++)
  294.      if (b->channel[i].enemy==q) b->channel[i].enemy=NULL;
  295.     b=b->next;
  296.     }
  297.  
  298. if (q==startlist)
  299.         {
  300.         a=startlist;
  301.         startlist=a->next;
  302.         q=NULL;
  303.         free(a);
  304.         } else
  305.         {
  306.         b=startlist;
  307.         while (b->next!=q) b=b->next;
  308.         b->next=q->next;
  309.         if (q==taillist) taillist=b;
  310.         free(q);
  311.         q=NULL;
  312.         }
  313.     
  314. } else
  315. {
  316.     startlist=NULL; 
  317.      taillist=NULL;
  318.     a=NULL;
  319.     b=NULL;
  320.     free(q);
  321.     q=NULL; 
  322. }
  323. if (alien) --noofaliens; else --noofplayers;
  324. if (noofplayers==0)
  325.      {
  326.     unlink(MASTERPIDFILE);
  327.     die();
  328.         }
  329. killcontrol();
  330. }
  331.  
  332.